package com.cwt_taxi_xa.protocol.card;
import com.cwt_taxi_xa.ActivityWaitQD;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.constants.SPConstants;
import com.cwt_taxi_xa.protocol.CardUtil;
import com.cwt_taxi_xa.protocol.DeviceBiz;
import com.cwt_taxi_xa.protocol.card.cmd.AckCallBack;
import com.cwt_taxi_xa.protocol.card.cmd.CardInfoEF08;
import com.cwt_taxi_xa.protocol.card.cmd.CardPictureEF05;
import com.cwt_taxi_xa.protocol.card.cmd.CardQrcodeEF06;
import com.cwt_taxi_xa.protocol.card.cmd.CardSMInfoEF04;
import com.cwt_taxi_xa.protocol.card.cmd.CardSupervisionEF0B;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.TTsFunction;
import com.cwt_taxi_xa.util.Tools;

/**
 * 刷卡检查模式
 * @author undi
 */
public class CardCheckInState extends CardBaseState {

	public CardCheckInState() {
		super();
		TAG = "CardCheckInState";
	}

	@Override
	public void startAndRead() {
		// TODO Auto-generated method stub
		//addCmdArray();
		super.startAndRead();
	}



	@Override
	public void start() {
		// TODO Auto-generated method stub
		addCmdArray();
		super.start();
		//AppLog.d(TAG,"start() ------------------" + AppLog.generateTag());
	}
	//可以在请求关机的
	@Override
	public void stop() {
		// TODO Auto-generated method stub
		super.stop();
	}
	/*
	 * 第一次读卡
	 */
	private void addCmdArray(){
		clearCmdArray();
		synchronized (cmdArray) {
			//出租汽车服务管理信息文件,西交投不用
			//addCmd(new CardSMInfoEF04("sminfo" ,carAckCallBack,DeviceBiz.getCarNumberSingleLetter()));
			addCmd(new CardInfoEF08("info" ,carAckCallBack));
			addCmd(new CardSupervisionEF0B("supervision" ,carAckCallBack,DeviceBiz.getCarNumberSingleLetter()));
			addCmd(new CardPictureEF05("picture" ,carAckCallBack));
			addCmd(new CardQrcodeEF06("qrcode" ,carAckCallBack));
		}
	}

	//需要考虑二次读卡逻辑
	@Override
	public void endCmd() {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"endCmd() ------------------");
		if(!SPUtils.getParamBoolean(SPConstants.SPK_signin, SPConstants.SPV_signin)){
			AppLog.d(TAG,"endCmd() ------------------");
			start();
			new Thread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						Thread.sleep(3000);
						CardUtil.sendTsm905Data(0x0020, null);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
			////此时发送无效
			//CardUtil.sendTsm905Data(0x0020, null);
		}
	}



	AckCallBack carAckCallBack = new AckCallBack() {

		@Override
		public void moveNext() {
			// TODO Auto-generated method stub
			moveNextCmd();
		}

		@Override
		public void callBack(String cmdTAG, String[] dataArray) {
			// TODO Auto-generated method stub
			AppLog.d(TAG, "carAckCallBack callBack TAG:" + cmdTAG);
			//基础信息 EF0B
			if("info".equals(cmdTAG)){
				//签到用到
				if(ActivityWaitQD.hasReadCard(dataArray[3])){
					//缓存读卡数据
					CardUtil.cardCache(dataArray);
					//clearCmdArray();
				}else{
					MainApplication.topActivity.sendMsg(10087, dataArray);
				}
				//首次刷卡
				moveNextCmd();
			}else if("sminfo".equals(cmdTAG)){
				//出租汽车服务管理信息
				moveNextCmd();
			}else if("supervision".equals(cmdTAG)){
				//判断是否读卡,先屏蔽
				if(CardUtil.hasReadCard()){
					AppLog.d(TAG,"二次读卡,继续获取服务监督卡");
					CardUtil.cardServerCache(dataArray);
					AppLog.d(TAG,"读卡 获取服务监督卡");
					MainApplication.topActivity.sendMsg(10099,CardUtil.readCard());
					clearCmdArray();
					return;
				}
				MainApplication.topActivity.sendMsg(10089, dataArray);
				moveNextCmd();
			}else{
				moveNextCmd();
			}
		}

		@Override
		public void sendCmd(int cmd, String data) {
			// TODO Auto-generated method stub
			sendTsm905Data(cmd, Tools.parseHexStr2Byte(data));
		}

		@Override
		public void error(String cmdTAG,int code, String error) {
			// TODO Auto-generated method stub
			TTsFunction.speakText(error);
			if(code == 1){
				//sendFirestCmd();
			}
			AppLog.d(TAG, "carAckCallBack error TAG:" + TAG);
		}
	};

	@Override
	public long delay() {
		// TODO Auto-generated method stub
		return 1000 * 8;
	}

	/*
	 *	二次读卡
	 */
	/*
	@Deprecated
	private void addCacheCmdArray(){
		clearCmdArray();
		synchronized (cmdArray) {
			addCmd(new CardSMInfoEF04("sminfo" ,carAckCallBack,DeviceBiz.getCarNumberSingleLetter()));
			addCmd(new CardInfoEF08("info" ,carAckCallBack));
		}
	}
	*/
}
